/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaPontos.java
 *
 * Created on Jun 24, 2011, 6:11:29 PM
 */
package cataletra.gui;

import cataletra.main.Main;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;

/**
 *
 * @author arthur
 */
public class JanelaPontos extends javax.swing.JDialog {
    
    private int pontuacao;
    private int palavrasAchadas;
    private int tempoRestante;
    private int total;          
    
    //---------TIMERS-------------
    private Timer timerPontuacao;
    private Timer timerPalavras;
    private Timer timerTempo;
    private Timer timerTotal;
    
    /** Creates new form JanelaPontos */
    public JanelaPontos(JFrame pai, boolean modal, int pontos, int palavras, int tempo) {
        super(pai, modal);
        inicializar(pai, pontos, palavras, tempo);
        this.total = pontos + tempo * 5 + palavras * 10;
        
        this.botaoNovoJogo.setVisible(false);
        this.botaoRecomecar.setVisible(false);
        
        this.iniciarTimers();
    }
    
    
    public JanelaPontos(JanelaPrincipal pai, boolean modal, int pontos, int palavras, boolean desistiu) {
        super(pai, modal);
        inicializar(pai, pontos, palavras, 0);        
        this.total = pontos + palavras * 10;
        
        labelFace.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/face-sad.png")));
        this.botaoProximoNivel.setVisible(false);
        this.botaoReiniciar.setVisible(false);
        this.botaoDesistir.setVisible(false);
        
        if(desistiu){
            this.labelParabens.setText("QUE PENA, VOCÊ PERDEU!");
        } else {
            this.labelParabens.setText("QUE PENA, ACABOU SEU TEMPO!");
        }
        
        this.iniciarTimers();
    }
    
    
    private void inicializar(JFrame pai, int pontos, int palavras, int tempo){        
        initComponents();
        this.setLocationRelativeTo(pai);
        
        this.labelPontuacao.setVisible(false);
        this.labelPalavrasAchadas.setVisible(false);
        this.labelTempoRestante.setVisible(false);
        this.labelTotal.setVisible(false);
        
        this.pontuacao = pontos;
        this.palavrasAchadas = palavras;
        this.tempoRestante = tempo;
    }
    
    
    private void iniciarTimers(){
        int delayPontuacao = 10;
        int delayTotal = 10;
        int delayTempo = 60;
        
        if(pontuacao > 500 && pontuacao <= 1200) delayPontuacao = 8;
        else if(pontuacao > 1200 && pontuacao < 1700) delayPontuacao = 6;
        else if(pontuacao > 1700) delayPontuacao = 4;
        
        if(total > 500 && total <= 1200) delayTotal = 8;
        else if(total > 1200 && total < 1700) delayTotal = 6;
        else if(total > 1700) delayTotal = 4;        
        
        if(tempoRestante > 100) delayTempo = 45;      
        
        ActionListener actionPontuacao = new ActionListener() { 
            int t = 0;
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if(t <= pontuacao){ labelPontuacao.setText(t + ""); }
                else{ timerPontuacao.stop();
                mostrarPontos();} t++;} };        
        this.timerPontuacao = new Timer(delayPontuacao, actionPontuacao);
        
        ActionListener actionPalavras = new ActionListener() { 
            int t = 0;
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if(t <= palavrasAchadas){ labelPalavrasAchadas.setText(t + " X 10"); }
                else{ timerPalavras.stop();
                mostrarPontos(); } t++;} };        
        this.timerPalavras = new Timer(60, actionPalavras);
        
        ActionListener actionTempo = new ActionListener() { 
            int t = 0;
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if(t <= tempoRestante){ labelTempoRestante.setText(t + " X 5"); }
                else{ timerTempo.stop();
                mostrarPontos(); } t++;} };        
        this.timerTempo = new Timer(delayTempo, actionTempo);
        
        ActionListener actionTotal = new ActionListener() { 
            int t = 0;
            @Override
            public void actionPerformed(java.awt.event.ActionEvent e) {
                if(t <= total){ labelTotal.setText(t + ""); }
                else{ timerTotal.stop();
                mostrarPontos(); } t++;} };        
        this.timerTotal = new Timer(delayTotal, actionTotal);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        labelFace = new javax.swing.JLabel();
        labelParabens = new javax.swing.JLabel();
        etiquetaPontuacao = new javax.swing.JLabel();
        etiquetaPalavrasAchadas = new javax.swing.JLabel();
        etiquetaTempoRestante = new javax.swing.JLabel();
        etiquetaTotal = new javax.swing.JLabel();
        botaoDesistir = new javax.swing.JButton();
        botaoReiniciar = new javax.swing.JButton();
        botaoNovoJogo = new javax.swing.JButton();
        botaoRecomecar = new javax.swing.JButton();
        botaoProximoNivel = new javax.swing.JButton();
        labelPontuacao = new javax.swing.JLabel();
        labelPalavrasAchadas = new javax.swing.JLabel();
        labelTempoRestante = new javax.swing.JLabel();
        labelTotal = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Nível Completado");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                janelaAtivada(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        labelFace.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/face-smile-big.png"))); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridheight = 4;
        getContentPane().add(labelFace, gridBagConstraints);

        labelParabens.setFont(new java.awt.Font("Verdana", 0, 24));
        labelParabens.setText("PARABÉNS! VOCÊ PASSOU DE NÍVEL");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(labelParabens, gridBagConstraints);

        etiquetaPontuacao.setText("PONTUAÇÃO");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(etiquetaPontuacao, gridBagConstraints);

        etiquetaPalavrasAchadas.setText("PALAVRAS ACHADAS");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(etiquetaPalavrasAchadas, gridBagConstraints);

        etiquetaTempoRestante.setText("TEMPO RESTANTE");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(etiquetaTempoRestante, gridBagConstraints);

        etiquetaTotal.setFont(new java.awt.Font("Dialog", 0, 17));
        etiquetaTotal.setText("TOTAL");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(etiquetaTotal, gridBagConstraints);

        botaoDesistir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/desistir22.png"))); // NOI18N
        botaoDesistir.setText("Desistir");
        botaoDesistir.setPreferredSize(new java.awt.Dimension(160, 30));
        botaoDesistir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoDesistir(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(15, 5, 5, 5);
        getContentPane().add(botaoDesistir, gridBagConstraints);

        botaoReiniciar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/reiniciar.png"))); // NOI18N
        botaoReiniciar.setText("Reiniciar");
        botaoReiniciar.setPreferredSize(new java.awt.Dimension(160, 30));
        botaoReiniciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoReiniciar(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.insets = new java.awt.Insets(15, 5, 5, 5);
        getContentPane().add(botaoReiniciar, gridBagConstraints);

        botaoNovoJogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/new.png"))); // NOI18N
        botaoNovoJogo.setText("Novo Jogo");
        botaoNovoJogo.setPreferredSize(new java.awt.Dimension(160, 30));
        botaoNovoJogo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoNovoJogo(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 6;
        getContentPane().add(botaoNovoJogo, gridBagConstraints);

        botaoRecomecar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/recomecar.png"))); // NOI18N
        botaoRecomecar.setText("Recomeçar");
        botaoRecomecar.setPreferredSize(new java.awt.Dimension(160, 30));
        botaoRecomecar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarBotaoRecomecar(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 6;
        getContentPane().add(botaoRecomecar, gridBagConstraints);

        botaoProximoNivel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/cataletra/images/next.png"))); // NOI18N
        botaoProximoNivel.setText("Próximo Nível");
        botaoProximoNivel.setPreferredSize(new java.awt.Dimension(160, 30));
        botaoProximoNivel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clicarProximoNivel(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(15, 5, 5, 5);
        getContentPane().add(botaoProximoNivel, gridBagConstraints);

        labelPontuacao.setText("X");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(labelPontuacao, gridBagConstraints);

        labelPalavrasAchadas.setText("Y X 10");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(labelPalavrasAchadas, gridBagConstraints);

        labelTempoRestante.setText("W X 10");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(labelTempoRestante, gridBagConstraints);

        labelTotal.setFont(new java.awt.Font("Dialog", 0, 17));
        labelTotal.setText("Z");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(labelTotal, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void janelaAtivada(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_janelaAtivada
        mostrarPontos();
    }//GEN-LAST:event_janelaAtivada

    private void clicarProximoNivel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarProximoNivel
        Main.getJanelaPrincipal().proximoNivel(tempoRestante);
        this.dispose();
    }//GEN-LAST:event_clicarProximoNivel

    private void clicarBotaoReiniciar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoReiniciar
        int resp = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja reiniciar esta partida?\nVocê perderá a pontuação que conseguiu.", "Atenção", JOptionPane.YES_NO_OPTION);
        
        if(resp == JOptionPane.YES_OPTION){   
            Main.getJanelaPrincipal().reiniciarPartida();
            this.dispose();
        }
    }//GEN-LAST:event_clicarBotaoReiniciar

    private void clicarBotaoNovoJogo(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoNovoJogo
        Main.getJanelaPrincipal().reiniciar();
        this.dispose();
    }//GEN-LAST:event_clicarBotaoNovoJogo

    private void clicarBotaoRecomecar(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoRecomecar
        Main.getJanelaPrincipal().iniciarJogo();
        this.dispose();
    }//GEN-LAST:event_clicarBotaoRecomecar

    private void clicarBotaoDesistir(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clicarBotaoDesistir
        int resp = JOptionPane.showConfirmDialog(this, "Tem certeza que deseja desistir desta partida?", "Atenção", JOptionPane.YES_NO_OPTION);
        
        if(resp == JOptionPane.YES_OPTION){   
            Main.getJanelaPrincipal().reiniciar();
            this.dispose();
        }
    }//GEN-LAST:event_clicarBotaoDesistir

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                JanelaPontos dialog = new JanelaPontos(null, true, 150, 16, 15);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoDesistir;
    private javax.swing.JButton botaoNovoJogo;
    private javax.swing.JButton botaoProximoNivel;
    private javax.swing.JButton botaoRecomecar;
    private javax.swing.JButton botaoReiniciar;
    private javax.swing.JLabel etiquetaPalavrasAchadas;
    private javax.swing.JLabel etiquetaPontuacao;
    private javax.swing.JLabel etiquetaTempoRestante;
    private javax.swing.JLabel etiquetaTotal;
    private javax.swing.JLabel labelFace;
    private javax.swing.JLabel labelPalavrasAchadas;
    private javax.swing.JLabel labelParabens;
    private javax.swing.JLabel labelPontuacao;
    private javax.swing.JLabel labelTempoRestante;
    private javax.swing.JLabel labelTotal;
    // End of variables declaration//GEN-END:variables

    public void mostrarPontos() {
        if(!labelPontuacao.getText().equals("" + pontuacao)){
            this.labelPontuacao.setVisible(true);
            this.timerPontuacao.start();
        } else if(!labelPalavrasAchadas.getText().equals(palavrasAchadas + " X 10")){
            this.labelPalavrasAchadas.setVisible(true);
            this.timerPalavras.start();
        } else if(!labelTempoRestante.getText().equals(tempoRestante + " X 5")){
            this.labelTempoRestante.setVisible(true);
            this.timerTempo.start();
        } else if(!labelTotal.getText().equals(total + " X 10")){
            this.labelTotal.setVisible(true);
            this.timerTotal.start();
        }
    }
    
}
